﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;

namespace DLULearningNetworkModel
{
    public enum EnumFeedType
    {
        PostOnWall = 1,
        ActivityPostOnWallFriend,
        ActivityMakeFriend,
        ActivityComment,
        ActivityPostOnCourseWall,
        PostOnCourseWall,
        ActivityChangeSeminarSchedule,
        ActivityChangeResourse
    }
    public partial class Feed
    {
        public static List<Feed> SelectFeedForAccountWall(Guid accountID, int numberOfSkip, int numberOfTake)
        {

            try
            {
                using (DLULNEntities db = new DLULNEntities())
                {
                    string type = EnumFeedType.PostOnWall.ToString();
                    var query = db.Feeds.Include("Account")
                        .Include("Account1")
                        .Include("Course")
                        .Include("FeedComments").Include("FeedComments.Account")
                        .Include("FeedComment")
                        .Include("FeedComment.Feed.Account")
                        .Include("FeedComment.Feed.Account1").Include("FeedComment.Feed.Course")
                        .Where(f => (f.PassiveAccountID_u == accountID && f.Type_s == type) || (f.Type_s.Contains("Activity") && f.ActiveAccountID_u == accountID))
                        .OrderByDescending(f => f.LastUpdate_dt).Skip(numberOfSkip).Take(numberOfTake);
                    return query.ToList();
                }
            }
            catch
            {
                return null;
            }
        }

        public static int Add(Feed newFeed)
        {
            try
            {
                DbTransaction trans = null;
                using (DLULNEntities db = new DLULNEntities())
                {
                    if (db.Connection.State == System.Data.ConnectionState.Closed)
                    {
                        db.Connection.Open();
                    }
                    trans = db.Connection.BeginTransaction();
                    db.Feeds.AddObject(newFeed);
                    //Neu feed la postonwall , neu feed.activeaccount != feed.passiveaccount them othernotification cho passiveaccount
                    EnumFeedType feedType = (EnumFeedType)Enum.Parse(typeof(EnumFeedType), newFeed.Type_s);
                    OtherNotification oNotification = null;
                    CourseNotification cNotification = null;
                    switch (feedType)
                    {
                        case EnumFeedType.ActivityPostOnWallFriend:
                            if (newFeed.ActiveAccountID_u != newFeed.PassiveAccountID_u)
                            {
                                oNotification = new OtherNotification();
                                oNotification.OtherNotificationID_u = Guid.NewGuid();
                                oNotification.AccountID_u = newFeed.PassiveAccountID_u.Value;
                                oNotification.FeedID_u = newFeed.FeedID_u;
                                oNotification.IsRead_b = false;
                                db.OtherNotifications.AddObject(oNotification);
                            }
                            break;
                        case EnumFeedType.ActivityPostOnCourseWall:
                            //Neu feed la postoncoursewall, notification cho tat ca thanh vien trong lop hoc, neu nguoi tao ra feed khac nguoi mo lop hoc thi notification cho nguoi moi lop
                            Course course = db.Courses.SingleOrDefault(c => c.CourseID_u == newFeed.CourseID_u);
                            if (course != null)
                            {
                                foreach (Enrollment enroll in course.Enrollments)
                                {
                                    if(enroll.AccountID_u != newFeed.ActiveAccountID_u )
                                    {
                                    cNotification = new CourseNotification();
                                    cNotification.CourseNotificationID_u = Guid.NewGuid();
                                    cNotification.AccountID_u = enroll.AccountID_u;
                                    cNotification.FeedID_u = newFeed.FeedID_u;
                                    cNotification.IsRead_b = false;
                                    db.CourseNotifications.AddObject(cNotification);
                                    }
                                }
                                // Nguoi dang khong phai nguoi mo lop
                                if (newFeed.ActiveAccountID_u != course.AccountID_u)
                                { 
                                    cNotification = new CourseNotification();
                                    cNotification.CourseNotificationID_u = Guid.NewGuid();
                                    cNotification.AccountID_u = course.AccountID_u;
                                    cNotification.FeedID_u = newFeed.FeedID_u;
                                    cNotification.IsRead_b = false;
                                    db.CourseNotifications.AddObject(cNotification);
                                }
                            }
                            break;
                        case EnumFeedType.ActivityComment:
                            //Neu feed la activitycomment
                            //Kiem tra comment.feed la postonwall thi chi co nhung comment tren feed khong phai cua minh va feed khong nam tren tuong minh thi moi notification
                            //Neu feed la activitycomment va comment.feed la postoncoursewall thi notification cho moi nguoi trong lop, neu nguoi comment khac nguoi mo lop hoc thi notification cho nguoi mo lop
                            FeedComment comment = db.FeedComments.SingleOrDefault(c => c.FeedCommentID_u == newFeed.CommentID);
                            #region Neu comment tren tuong cua ban be
		if (comment.Feed.Type_s == EnumFeedType.PostOnWall.ToString())
                            {
                                if (comment.Feed.ActiveAccountID_u != comment.ActiveAccount_u)
                                {
                                    oNotification = new OtherNotification();
                                    oNotification.OtherNotificationID_u = Guid.NewGuid();
                                    oNotification.AccountID_u = comment.Feed.ActiveAccountID_u;
                                    oNotification.FeedID_u = newFeed.FeedID_u;
                                    oNotification.IsRead_b = false;
                                    db.OtherNotifications.AddObject(oNotification);
                                    if (comment.Feed.PassiveAccountID_u != comment.ActiveAccount_u && comment.Feed.PassiveAccountID_u != comment.Feed.ActiveAccountID_u)
                                    {
                                        oNotification = new OtherNotification();
                                        oNotification.OtherNotificationID_u = Guid.NewGuid();
                                        oNotification.AccountID_u = comment.Feed.PassiveAccountID_u.Value;
                                        oNotification.FeedID_u = newFeed.FeedID_u;
                                        oNotification.IsRead_b = false;
                                        db.OtherNotifications.AddObject(oNotification);
                                    }
                                }
                                else
                                {
                                    if (comment.Feed.PassiveAccountID_u != comment.ActiveAccount_u)
                                    {
                                        oNotification = new OtherNotification();
                                        oNotification.OtherNotificationID_u = Guid.NewGuid();
                                        oNotification.AccountID_u = comment.Feed.PassiveAccountID_u.Value;
                                        oNotification.FeedID_u = newFeed.FeedID_u;
                                        oNotification.IsRead_b = false;
                                        db.OtherNotifications.AddObject(oNotification);
                                    }
                                }
                            } 
	#endregion
                            #region Neu comment tren tuong cua lop hoc
                            Course ncourse = db.Courses.SingleOrDefault(c => c.CourseID_u == comment.Feed.CourseID_u);
                            if (ncourse != null)
                            {
                                foreach (Enrollment enroll in ncourse.Enrollments)
                                {
                                    if(enroll.AccountID_u != newFeed.ActiveAccountID_u )
                                    {
                                    cNotification = new CourseNotification();
                                    cNotification.CourseNotificationID_u = Guid.NewGuid();
                                    cNotification.AccountID_u = enroll.AccountID_u;
                                    cNotification.FeedID_u = newFeed.FeedID_u;
                                    cNotification.IsRead_b = false;
                                    db.CourseNotifications.AddObject(cNotification);
                                    }
                                }
                                // Nguoi dang khong phai nguoi mo lop
                                if (newFeed.ActiveAccountID_u != ncourse.AccountID_u)
                                { 
                                    cNotification = new CourseNotification();
                                    cNotification.CourseNotificationID_u = Guid.NewGuid();
                                    cNotification.AccountID_u = ncourse.AccountID_u;
                                    cNotification.FeedID_u = newFeed.FeedID_u;
                                    cNotification.IsRead_b = false;
                                    db.CourseNotifications.AddObject(cNotification);
                                }
                            }
                            #endregion
                            break;
                    }
                    if (db.SaveChanges() > 0)
                    {
                        trans.Commit();
                        return 1;
                    }
                    else
                    {
                        trans.Rollback();
                        return 0;
                    }
                }
            }
            catch
            {
                return 101;
            }
        }
        /// <summary>
        /// Update thoi gian cap nhat cuoi cung
        /// </summary>
        /// <param name="feedID"></param>
        /// <returns>0: khong co feed nay, 1: thanh cong, 2: savechange =0, 101: catch,</returns>
        public static int UpdateLastDate(Guid feedID)
        {
            using (DLULNEntities db = new DLULNEntities())
            {
                Feed feed = db.Feeds.SingleOrDefault(f => f.FeedID_u == feedID);
                if (feed != null)
                {
                    feed.LastUpdate_dt = DateTime.Now;
                    if (db.SaveChanges() > 0)
                    {
                        return 1;
                    }
                    else
                    {
                        return 2;
                    }
                }
                else
                {
                    return 0;
                }
            }
        }

        public static Feed SelectByFeedID(Guid feedID)
        {
            try
            {
                using (DLULNEntities db = new DLULNEntities())
                {
                    return db.Feeds.Include("Account").Include("Account1")
                        .Include("Course").Include("FeedComments")
                        .Include("FeedComments.Account")
                        .Include("FeedComment")
                        .Include("FeedComment.Feed.Account")
                        .Include("FeedComment.Feed.Account1").Include("FeedComment.Feed.Course")
                        .SingleOrDefault(f => f.FeedID_u == feedID);
                }
            }
            catch
            {
                return null;
            }
        }

        public static int DeleteFeed(Guid feedID)
        {
            try
            {
                using (DLULNEntities db = new DLULNEntities())
                {
                    Feed existingFeed = db.Feeds.SingleOrDefault(f => f.FeedID_u == feedID);
                    if (existingFeed != null)
                    {
                        foreach (FeedComment fcmt in existingFeed.FeedComments)
                        {
                            Feed feedActivity = db.Feeds.SingleOrDefault(f => f.CommentID == fcmt.FeedCommentID_u);
                            if (feedActivity != null)
                            {
                                db.Feeds.DeleteObject(feedActivity);
                            }
                        }

                        db.Feeds.DeleteObject(existingFeed);
                        if (db.SaveChanges() > 0)
                        {
                            return 1;
                        }

                    }
                    return 0;
                }
            }
            catch
            {
                return 101;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="CommentID"></param>
        /// <returns>0: không thanh cong, 1: thanh cong, 2: khong co, 101: catch</returns>
        public static int DeleteFeedByCommentID(Guid CommentID)
        {
            try
            {
                using (DLULNEntities db = new DLULNEntities())
                {
                    var lstFeed = db.Feeds.Where(f => f.CommentID == CommentID);
                    if (lstFeed.Count() == 0)
                        return 2;
                    foreach (var f in lstFeed)
                    {
                        db.Feeds.DeleteObject(f);
                    }
                    if (db.SaveChanges() > 0)
                    {
                        return 1;
                    }
                    return 0;
                }
            }
            catch
            {
                return 101;
            }
        }

        public static List<Feed> SelectFeedForCourseWall(Guid courseID, int numberOfSkip, int numberOfTake)
        {
            try
            {
                using (DLULNEntities db = new DLULNEntities())
                {
                    string typePostCourseWall = EnumFeedType.PostOnCourseWall.ToString();//&& f.Type_s == type
                    string typeActivityChangeResource = EnumFeedType.ActivityChangeResourse.ToString();
                    string typeActivityChangeSchedule = EnumFeedType.ActivityChangeSeminarSchedule.ToString();
                    var query = db.Feeds.Include("Account")
                        .Include("Course")
                        .Include("FeedComments").Include("FeedComments.Account")
                        .Where(f => f.CourseID_u == courseID &&(f.Type_s== typePostCourseWall ||f.Type_s == typeActivityChangeResource || f.Type_s == typeActivityChangeSchedule))
                        .OrderByDescending(f => f.LastUpdate_dt).Skip(numberOfSkip).Take(numberOfTake);
                    return query.ToList();
                }
            }
            catch
            {
                return null;
            }
        }

    }
}
